############Replication file for "Who Benefits"
# date: 2/17/2025

##load packages
library(tidyverse)
library(viridis)
library(haven)
library(labelled)
library(labeling)
library(modelsummary)
library(gridExtra)

########## T-tests/Difference in Means from Results
#load data
gender <- read_csv("Data/gender_df.csv")

gender$amt_diff <- (gender$APP_2_amt_1 - gender$APP_1_amt_1)
summary(gender$amt_diff)


Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

gender <- gender%>%
  group_by(applicant2_treat)%>%
  mutate(app_2_amt_av = mean(APP_2_amt_1))%>%
  mutate(app_1_amt_av = mean(APP_1_amt_1))%>%
  mutate(STATE_amt_av = mean(STATE_amt_1))

#Exc Misty to Baseline <- one group
gender <- gender %>%
  subset(applicant2_treat < 7)
excmist <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 1)
sandra <- excmist$APP_1_amt_1
mean(sandra)
emist <- excmist$APP_2_amt_1
res1 <- t.test(sandra, emist, paired = TRUE)
res1

#Poor Misty to Baseline <- one group
poormist <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 2)
sandra2 <- poormist$APP_1_amt_1
mean(sandra2)

pmist <- poormist$APP_2_amt_1
res2 <- t.test(sandra2, pmist, paired = TRUE)
res2

#Exc James to Baseline
excjames <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 3)
sandra3 <- excjames$APP_1_amt_1
ejames <- excjames$APP_2_amt_1
res3 <- t.test(sandra3, ejames, paired = TRUE)
res3

#Poor James to Baseline
poorjames <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 4)
sandra4 <- poorjames$APP_1_amt_1
mean(sandra4)
pjames <- poorjames$APP_2_amt_1
res4 <- t.test(sandra4, pjames, paired = TRUE)
res4

#Exc Sammie to Baseline
excsam <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 5)
sandra5 <- excsam$APP_1_amt_1
esam <- excsam$APP_2_amt_1
res5 <- t.test(sandra5, esam, paired = TRUE)
res5

#Poor Sammie to Baseline
poorsam <- gender %>%
  select(applicant2_treat, APP_1_amt_1, APP_2_amt_1) %>%
  filter(applicant2_treat == 6)
sandra6 <- poorsam$APP_1_amt_1
mean(sandra6)
psam <- poorsam$APP_2_amt_1
res6 <- t.test(sandra6, psam, paired = TRUE)
res6

#Excellent Misty to Poor Misty
res7 <- t.test(emist, pmist, paired = FALSE)
res7

#Excellent James to Poor James
res8 <- t.test(ejames, pjames, paired = FALSE)
res8

#Excellent Sammie to Poor Sammie
res9 <- t.test(esam, psam, paired = FALSE)
res9

#Excellent Misty to Excellent Sam
res10 <- t.test(emist, esam, paired = FALSE)
res10

#Poor Misty to Poor Sam
res11 <- t.test(pmist, psam, paired = FALSE)
res11

#Excellent James to Excellent Sam
res12 <- t.test(ejames, esam, paired = FALSE)
res12
#Poor James to Poor Sam
res13 <- t.test(pjames, psam, paired = FALSE)
res13

#Excellent Misty to Excellent James
res14 <- t.test(emist, ejames, paired = FALSE)
res14

#Poor Misty to Poor James
res15 <- t.test(pmist, pjames, paired = FALSE)
res15



#### Results for Table 1
res16 <- t.test(sandra, sandra2, paired = FALSE)
res16
res17 <- t.test(sandra3, sandra4, paired = FALSE)
res17
res18 <- t.test(sandra5, sandra6, paired = FALSE)
res18

tests2 <- list(res7, res8, res9)
tab2 <- sapply(tests2, function(x) {
  c(x$estimate[1],
    x$estimate[2],
    ci.lower = x$conf.int[1],
    ci.upper = x$conf.int[2],
    p.value = x$p.value)
})

tab2 <- as.data.frame(tab2) %>%
  rename("Excellent Misty vs Poor Misty" = V1)%>%
  rename("Excellent James vs Poor James" = V2)%>%
  rename("Excellent Sammie vs Poor Sammie" = V3) %>%
  mutate(measure = c("Mean 1", "Mean 2", "Upper CI", "Lower CI",
                     "p-value")) %>%
  pivot_longer(
    cols = !measure,
    names_to = "Condition",
    values_to = "Results"
  ) %>%
  pivot_wider(names_from = "measure", values_from = "Results")

round_df <- function(x, digits) {
  # round all numeric variables
  # x: data frame 
  # digits: number of digits to round
  numeric_columns <- sapply(x, mode) == 'numeric'
  x[numeric_columns] <-  round(x[numeric_columns], digits)
  x
}

tab2 <- round_df(tab2, 3)

#### Table 1
stargazer::stargazer(tab2, type = "latex", style = "ajps", 
                     summary = FALSE,
                     font.size = 'footnotesize', 
                     column.sep.width = "1pt", 
                     no.space=T,
                     digits.extra=3)


## no name vs sandra
nn <- read_csv("Data/gender_df.csv") %>%
  subset(applicant2_treat == "7")

nn <- nn%>%
  group_by(applicant2_treat)%>%
  mutate(app_2_amt_av = mean(APP_2_amt_1))%>%
  mutate(app_1_amt_av = mean(APP_1_amt_1))%>%
  mutate(STATE_amt_av = mean(STATE_amt_1))

##t-test Sandra vs. no name
s <- nn$APP_1_amt_1
no_name <- nn$APP_2_amt_1
res_robust <- t.test(s, no_name, paired = FALSE)
res_robust


######################## Figures
for_fig2 <- gender %>%
  select(applicant2_treat, app_1_amt_av, app_2_amt_av, STATE_amt_av, 
         applicant_2_name, applicant_2_rate, amt_diff, applicant_2_sex)%>%
  pivot_longer(app_1_amt_av:STATE_amt_av, names_to = "person", 
               values_to = "amount")

for_fig2 <- for_fig2 %>%
  group_by(applicant2_treat) %>%
  mutate(se = (sd(amount)/sqrt(length((amount))))) %>%
  filter(applicant2_treat < 7) #drop no-name


##Figure 2
ggplot(gender, aes(x=APP_1_amt_1, y=APP_2_amt_1)) + 
  geom_point() +
  theme_bw()


df2 <- select(gender, -applicant_2_char)
ggplot(gender, aes(x=APP_1_amt_1, y=APP_2_amt_1)) + theme_bw() +
  geom_point(data = df2, color = "grey70", alpha=.4) +
  geom_point(alpha=.6, aes(color = applicant_2_char)) + 
  scale_color_manual(values=c("#000000", "#000000", "#000000",
                              "#000000", "#000000", "#000000",
                              "#000000")) +
  facet_wrap(~applicant_2_char) +
  theme(legend.position = "none") +
  labs(x = " $$ Given to Sandra (Exc)",
       y = "$$ Given to Treatment",
       color = "Treatment")
#ggsave("Paper/figs/dollars-given-by-treatment-condition.png", 
#      height = 4, width = 6)



###Figure 3

ggplot(for_fig2, aes(x=person, y=amount, color = applicant_2_rate)) + 
  geom_point(aes(shape = person)) +
  facet_wrap(vars(applicant_2_name)) +
  geom_errorbar(aes(ymin =amount - 1.96*se, 
                    ymax=amount + 1.96*se,  width = 0.3)) +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_shape_discrete(labels=c('Sandra (S)', 'Treatment (T)', 'Budget Deficit (BD)')) +
  scale_x_discrete(labels=c("app_1_amt_av" = "S", "app_2_amt_av" = "T",
                            "STATE_amt_av" = "BD")) +
  #theme(axis.text.x = element_blank()) +
  #  theme(legend.key.height = unit(.9, 'mm'),
  #         legend.position = c(.85,.4)) +
  labs(x = "",
       y = "Average Dollars Awarded",
       shape = "Payee",
       color = "Quality of Treatment Name")
#ggsave("Paper/figs/general-results-name.png", height = 4, width = 6)



########################################## No-Name Figure
rm(list=ls())
gender <- read_csv("Data/gender_df_w_no_name.csv")

gender$amt_diff <- (gender$APP_2_amt_1 - gender$APP_1_amt_1)

gender <- gender%>%
  group_by(applicant2_treat)%>%
  mutate(app_2_amt_av = mean(APP_2_amt_1))%>%
  mutate(app_1_amt_av = mean(APP_1_amt_1))%>%
  mutate(STATE_amt_av = mean(STATE_amt_1))

for_fig2 <- gender %>%
  select(applicant2_treat, app_1_amt_av, app_2_amt_av, STATE_amt_av, 
         applicant_2_name, applicant_2_rate, amt_diff, applicant_2_sex)%>%
  pivot_longer(app_1_amt_av:STATE_amt_av, names_to = "person", 
               values_to = "amount")

for_fig2 <- for_fig2 %>%
  group_by(applicant2_treat) %>%
  mutate(se = (sd(amount)/sqrt(length((amount)))))

for_fig2 <- for_fig2 %>%
  subset(applicant2_treat == 7)

ggplot(for_fig2, aes(x=person, y=amount, color = applicant_2_rate)) + 
  geom_point(aes(shape = person)) +
  facet_wrap(vars(applicant_2_name)) +
  geom_errorbar(aes(ymin =amount - 1.96*se, 
                    ymax=amount + 1.96*se,  width = 0.3)) +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_shape_discrete(labels=c('Sandra (S)', 'Treatment (T)', 'Budget Deficit (BD)')) +
  scale_x_discrete(labels=c("app_1_amt_av" = "S", "app_2_amt_av" = "T",
                            "STATE_amt_av" = "BD")) +
  #theme(axis.text.x = element_blank()) +
  #  theme(legend.key.height = unit(.9, 'mm'),
  #         legend.position = c(.85,.4)) +
  labs(x = "",
       y = "Average Dollars Awarded",
       shape = "Payee",
       color = "Quality of Treatment Name") +
  theme(legend.key.height = unit(.7, 'mm'),
        legend.position = c(.2,.15))
#ggsave("Paper/figs/no-name-result.png", height = 6, width = 6)



####################################### Regression Results
rm(list=ls())
df <- read_csv("Data/data_for_regression.csv")

basic <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate, data = df)
summary(basic)

interact <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate +
                 applicant_2_sex*applicant_2_rate, data = df)
summary(interact)

controls_basic <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate + 
                       respondent_female + respondent_nonwhite + ideo5, data = df)
summary(controls_basic)

controls_interact <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate +
                          applicant_2_sex*applicant_2_rate + 
                          respondent_female + respondent_nonwhite + ideo5, data = df)
summary(controls_interact)

##Appendix Table 1
stargazer::stargazer(basic, interact, controls_basic, controls_interact,
                     title="Amounts Awarded to Applicant 2", 
                     type="latex", style = "apsr",
                     #out="ols1.tex",
                     font.size = 'footnotesize', 
                     column.sep.width = "1pt", 
                     no.space=T,
                     covariate.labels = c("Male",
                                          "Rated Poor",
                                          "Rated Poor X Male",
                                          "Respondent Female",
                                          "Respondent Nonwhite", 
                                          "Respondent Ideology"),
                     dep.var.labels = c("Amount Given to Applicant 2"))



######with weights

##how many obs in low-ses
oversample <- df %>%
  select(weight_oversample) %>%
  drop_na()
#590

#how many obs in genpop
genpop <- df %>%
  select(weight_genpop) %>%
  drop_na()
#1095

#weight the weights
weight_genpop <- genpop$weight_genpop*(1095/(1095+590))
weight_oversample <- oversample$weight_oversample*(590/(1095+590))
weight_full <- c(weight_genpop, weight_oversample)
df$weight_full <- weight_full #dfs are sorted correctly already


weights_basic <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate, weights = weight_full,
                    data = df)
summary(weights_basic)

weights_interact <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate +
                         applicant_2_sex*applicant_2_rate, weights = weight_full,
                       data = df)
summary(weights_interact)

weights_controls_basic <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate + 
                               respondent_female + respondent_nonwhite + ideo5, weights = weight_full,
                             data = df)
summary(weights_controls_basic)

weights_controls_interact <- lm(APP_2_amt_1 ~ applicant_2_sex + applicant_2_rate +
                                  applicant_2_sex*applicant_2_rate + 
                                  respondent_female + respondent_nonwhite + ideo5, 
                                weights = weight_full,
                                data = df)
summary(weights_controls_interact)


##Appendix Table 2
stargazer::stargazer(weights_interact, weights_controls_interact,
                     title="Amounts Awarded to Applicant 2", 
                     type="latex", style = "apsr",
                     #out="ols2.tex",
                     font.size = 'footnotesize', 
                     column.sep.width = "1pt", 
                     no.space=T,
                     covariate.labels = c("Male",
                                          "Rated Poor",
                                          "Respondent Female",
                                          "Respondent Nonwhite", 
                                          "Respondent Ideology",
                                          "Rated Excellent X Male"),
                     dep.var.labels = c("Amount Given to Applicant 2"))


############## Appendix Figure 1
names <- read.csv("Data/names.csv") %>%
  subset(char == "working class" | char == "professional" |
           char == "likable" | char == "honest" |
           char == "hardworking" | char == "competent")

ggplot(names, aes(x=char, y=est)) + 
  geom_errorbar(aes(x=char, ymin=est-se, ymax=est+se, color=name), 
                width=0.4, size=1) +
  geom_point(aes(color=name, shape = ), size=3, alpha=.8) +
  coord_flip() + theme_minimal() +
  scale_color_viridis_d() +
  labs(title = "Name Characteristics",
       subtitle = "From Hayes and Mitchell (2022)",
       x = "Characteristics",
       y = "Estimates",
       color = "Name")
